##import hkdf
import hashlib
import sys
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.kdf.hkdf import HKDF,HKDFExpand
from cryptography.hazmat.primitives import hashes, hmac
from binascii import hexlify, unhexlify
import base64


def GetPsk():
    k_encryptedPsk = bytes.fromhex("7471572b566d526b3067525a48586878535658676a513d3d")
    k_pskKey = bytes.fromhex("354d36504279394171304176635677654968484636673d3d")

    base64_encryptedPsk = base64.b64decode(k_encryptedPsk)
    base64_pskKey = base64.b64decode(k_pskKey)

    psk = bytes([a ^ b for a, b in zip(base64_encryptedPsk, base64_pskKey)])
    print("psk:")
    print(psk)
    print("")
    return psk

def GetAesKey():
    aesKey_len = int(32)
    # 填入salt值
    aesKey_salt = bytes("xGThg5u38B/nHmbR", "utf-8")
    # 填入manuKey
    aesKey_manuKey = bytes.fromhex("4B3F3F683F203F3F7E4D3F3F3F223F3645693F791F153F650F3F1E133F3F123F3F3F63463F153F3F3F3F4D1F4B4A3E3F")
    # 填入productId
    aesKey_proId = bytes("OH0000OW", "utf-8")
    aesKey_info = aesKey_manuKey + aesKey_proId
    aesKey_ikm = GetPsk()
    aesKey_hkdf = HKDF(hashes.SHA256(), hashes.SHA256().digest_size, aesKey_salt, None, default_backend())
    prk = aesKey_hkdf._extract(aesKey_ikm)
    aesKey_Expand_hkdf = HKDFExpand(hashes.SHA256(), aesKey_len, aesKey_info, default_backend())
    okm = aesKey_Expand_hkdf.derive(prk)
    print("okm:")
    print(okm)
    print("")
    for i in okm:
        print("%02x" % i, end="")
    print("")
    print("key:")
    for i in okm[0:16]:
        print("%02x" % i, end="")
    print("")
    print("iv:")
    for i in okm[16:32]:
        print("%02x" % i, end="")
    print("")
    print("salt:")
    for i in aesKey_salt:
        print("%02x" % i, end="")
    print("")
    return

# main
GetAesKey()
# 在线AES加解密网站
# https://www.mklab.cn/utils/aes